package com.smartHire.controller.Business;

import com.alibaba.fastjson.JSONObject;
import com.smartHire.domain.Goods;
import com.smartHire.service.GoodsService;
import com.smartHire.util.HttpRequest;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;



@WebServlet(name = "RemoveGoodsServlet", value = "/RemoveGoodsServlet")
public class RemoveGoodsServlet extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        int Good_id = Integer.parseInt(request.getParameter("goods_id"));
        System.out.println(Good_id);
        WebApplicationContext ctx = null;
        //使用框架中的方法，获取容器对象
        ServletContext sc = getServletContext();
        ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
        System.out.println("容器对象的信息========" + ctx);
        //获取service

        try{
            GoodsService goodsServiceservice  = (GoodsService) ctx.getBean("goodsService");

            Goods goods = new Goods();

            goods.setGood_id(Good_id);

            if(goodsServiceservice.queryGoodsByID(goods)!=null){
                System.out.println("找到");
                if(goodsServiceservice.deleteGoods(goods)==1){
                    System.out.println(2);
                    response.getWriter().write("cg");
                }
                else
                {
                    System.out.println(3);
                    response.getWriter().write("222222");
                }
            }
            else
            {
                System.out.println(4);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

    }

}
